﻿@model OrchardHUN.Scripting.Models.ScriptPart
@{
    Style.Require("ScriptingAdmin");
    Script.Require("jQuery").AtFoot();
    Script.Include(Url.Content("~/Themes/TheAdmin/Scripts/admin.js")).AtFoot();
    Script.Require("AceEditor.NoConflict").AtFoot(); // Locally PHP syntax highlighting is not working and can't use CDN here: https://github.com/OrchardCMS/Orchard/issues/3158
}

<fieldset>
    <legend>@T("Scripting")</legend>
    @Html.HiddenFor(m => m.Expression)
    @Html.LabelFor(m => m.Engine, T("Scripting engine"))
    @Html.DropDownListFor(m => m.Engine,
        new SelectList(Model.RegisteredEngines.Select(engine => new SelectListItem { Text = engine.DisplayName.ToString(), Value = engine.Name }), "Value", "Text"))
    <div id="orchardhun-scripting-editor-@Model.ContentItem.Id" class="orchardhun-scripting-editor">@Model.Expression</div>
</fieldset>
<fieldset id="orchardhun-scripting-help-@Model.ContentItem.Id">
    <legend>@T("Help")</legend>
    <div>
        <div id="orchardhun-scripting-editor-sample" class="orchardhun-scripting-editor"></div>
        <span id="orchardhun-scripting-hint" class="hint"></span>
        <input type="button" id="orchardhun-scripting-copyscript" class="button" value="@T("Copy example to editor")" />
    </div>
</fieldset>

@using (Script.Foot())
{
    <script type="text/javascript">
        (function ($) {
            $(function () {
                $("#orchardhun-scripting-help-@Model.ContentItem.Id legend")
                    .expandoControl(function (controller) { return controller.next("div"); }, { collapse: true, remember: true });

                var engine = $("#@Html.FieldIdFor(m => m.Engine)");
                var editor = ace.edit("orchardhun-scripting-editor-@Model.ContentItem.Id");
                var expression = $("#@Html.FieldIdFor(m => m.Expression)");

                var sample = ace.edit("orchardhun-scripting-editor-sample");
                sample.setReadOnly(true);
                var hint = $("#orchardhun-scripting-hint");
                var help = { "sample": sample, "hint": hint };

                $(expression[0].form).submit(function () {
                    expression.val(editor.getValue());
                });

                function triggerSelectedEngineChanged() {
                    $(document).triggerHandler({
                        type: "selectedEngineChanged.ScriptingExtensions",
                        engine: engine.val(),
                        editor: editor,
                        help: help
                    });
                    sample.renderer.updateFull(true);
                }

                engine.change(function () {
                    triggerSelectedEngineChanged();
                });

                triggerSelectedEngineChanged();

                $("#orchardhun-scripting-copyscript").click(function () {
                    editor.getSession().setValue(sample.getSession().getValue());
                });
            });
        })(jQuery);
    </script>
}